﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblBookingDAO
    {
        private DBService _db = null;
        public TblBookingDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool Insert(TblBooking booking, List<TblBookingDetail> lstDetail, out int BookID)
        {
            BookID = 0;
            bool flag = false;
            try
            {

                DataTable dt = _db.ExeSP_DataTable("sp_TblBookingIns",
                   new string[] { "@OrderDate", "@DateActive", "@Status", "@Notes", "@AccountID", "@Total", "@ShippingMethod" },
                   new object[] { 
                                       Convert.ToDateTime(booking.DateBook),
                                       Convert.ToDateTime(booking.DateActive),
                                        Code.FixIntNull(booking.Status).ToString(),
                                        Code.FixStringNull(booking.Notes),
                                        Code.FixIntNull(booking.AccountID).ToString(),
                                        Convert.ToDecimal(booking.TotalPrice),
                                        Code.FixIntNull(booking.PaymentMethod),
                 
                    }, CommandType.StoredProcedure);

                foreach (TblBookingDetail bookingDetail in lstDetail)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        bookingDetail.TotalAmount = bookingDetail.Quantity * bookingDetail.Price;

                        _db.ExeSP_DataTable("sp_TblBookingDetail_Ins",
                          new string[] { "@BookID", "@ItemID", "@Quantity", "@Price", "@TotalAmount", "@Discount" },
                          new object[] { dr["BookID"],    
                                    Code.FixIntNull(bookingDetail.ItemID).ToString(),
                                    Code.FixIntNull(bookingDetail.Quantity).ToString(),
                                    Convert.ToDecimal(bookingDetail.Price), 
                                    Convert.ToDecimal(bookingDetail.TotalAmount),
                                    bookingDetail.Discount},
                                         CommandType.StoredProcedure);

                        BookID = Convert.ToInt32(dr["BookID"].ToString());
                    }

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public DataTable GetBooking(int BookID)
        {
            DataTable dt = null;
            dt = _db.ExeSP_DataTable("Select * from TblBooking where BookID = @BookID", 
                new string[] { "@BookID" }, 
                new object[] { BookID }, CommandType.Text);

            return dt;
        }

        public DataTable GetOrderList(string p_OrderDateFrom, string p_OrderDateTo, string p_Email, int status)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_Order_List",
                    new string[] { "@p_OrderDateFrom", "@p_OrderDateTo", "@p_Email", "@p_Status" },
                    new object[] { p_OrderDateFrom, p_OrderDateTo, p_Email, status }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        // Get Order Theo Id

        public DataTable GetOrder(int id)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_TblOrder_Get",
                    new string[] { "@OrderId" }
                    , new object[] { id }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Update Status
        public DataTable Update(int Status, int BookID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("Update TblBooking set Status= @Status where BookID = @BookID",
                    new string[] { "@Status", "@BookID" },
                    new object[] { Status, BookID }, CommandType.Text);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

    }
}
